package com.qf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qf.dao.QfCourseDao;
import com.qf.pojo.RedisKey;
import com.qf.pojo.respVo.BaseResp;
import com.qf.pojo.vo.QfCourse;
import com.qf.service.CourseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class CourseServiceImpl implements CourseService {


    @Autowired
    QfCourseDao qfCourseDao;

    @Autowired
    RedisTemplate redisTemplate;

    private static Logger logger = LoggerFactory.getLogger(CourseServiceImpl.class);

    @Override
    public BaseResp finCourseByClassify(Integer id, Integer page, Integer size) {
        //1.判断redis中是否有该key值
        Boolean aBoolean = redisTemplate.hasKey(RedisKey.CLASSFIY + id.toString());
        List range=null;
        Integer start =(page-1)*size;
        Long total=null;
        if (aBoolean){
            logger.debug("查询课程分类为："+id+"的数据，从redis中获取");
            //分页查询,计算出起始的行数
            range= redisTemplate.opsForList().range(RedisKey.CLASSFIY + id.toString(), start, start + size-1);
            total = redisTemplate.opsForList().size(RedisKey.CLASSFIY + id.toString());
        }else{
            //1.从数据库查询出该分类的所有的课程
            logger.debug("查询课程分类为："+id+"的数据，从数据库中获取");
           List<QfCourse> courses = qfCourseDao.findByClassfiy(id);
            /**
             * 放置缓存穿透问题 1.放置一个null值，同时必须设置过期时间
             */
           if (courses==null||courses.size()==0){
               redisTemplate.opsForList().leftPush(RedisKey.CLASSFIY + id.toString(),"null");
               redisTemplate.expire(RedisKey.CLASSFIY + id.toString(),1, TimeUnit.MINUTES);
           }else{
           //放置到redis中
            redisTemplate.opsForList().leftPushAll(RedisKey.CLASSFIY + id.toString(),courses);
            range= redisTemplate.opsForList().range(RedisKey.CLASSFIY + id.toString(), start, start + size);
            total = redisTemplate.opsForList().size(RedisKey.CLASSFIY + id.toString());

           }
        }

        return new BaseResp(1,"查询成功！",range,total);
    }

    @Override
    public BaseResp findCourseDetailById(Integer id) {

        //1.判断redis中是否有该最外层key
        Boolean aBoolean = redisTemplate.hasKey(RedisKey.COURSEDETAILLIST);


        QfCourse qfCourse=null;
        if (aBoolean){
            Object o = redisTemplate.opsForHash().get(RedisKey.COURSEDETAILLIST, id.toString());
            if (o==null){
                 qfCourse = qfCourseDao.findCourseDetailById(id);
                //存储在redis中
                redisTemplate.opsForHash().put(RedisKey.COURSEDETAILLIST,qfCourse.getId().toString(),qfCourse);
            }else{
                //从redis中获取该value
                Object o1 = redisTemplate.opsForHash().get(RedisKey.COURSEDETAILLIST, id.toString());
                //转换
                String s = JSONObject.toJSONString(o1);
                //使用jsonobject.parseObject
                 qfCourse = JSONObject.parseObject(s, QfCourse.class);
            }

        }else{
             qfCourse = qfCourseDao.findCourseDetailById(id);
            //存储在redis中
            redisTemplate.opsForHash().put(RedisKey.COURSEDETAILLIST,qfCourse.getId().toString(),qfCourse);
        }

        if (qfCourse==null){
            return new BaseResp(0,"没有该课程！",null,null);
        }
        return new BaseResp(1,"查询成功！",qfCourse,null);
    }
}
